#!/usr/bin/perl
#copyright Ian Baker 2007

use Device::SerialPort;
use Time::HiRes qw (usleep);

$if0 = new Device::SerialPort('/dev/ttyS0', 0);

$if0->baudrate(19200);
$if0->parity("odd");
$if0->databits(8);
$if0->stopbits(1);
$if0->handshake('none');

die "$!" unless $if0->write_settings;

$if1 = new Device::SerialPort('/dev/ttyS1', 0);

$if1->baudrate(19200);
$if1->parity("odd");
$if1->databits(8);
$if1->stopbits(1);
$if1->handshake('none');

die "$!" unless $if1->write_settings;

my ($read_count, @reads);

my $minus = 1;
while(1) {
    if($minus == 0) {
      $minus = 1;
    } else {
      $minus = 0;
    }
    
    foreach my $addr (33..95) {
	next if $addr%2 == $minus;
	$if0->write(chr($addr));
#        $if0->write(chr($addr - $minus));
	#($count, $in) = $if1->read(1);
	#print "$addr: " . ord($in) . " / $count\n";
        #$read_count++;
        #push @reads, ord($in);
        #if($read_count == 1000) {
        #  my $sum;
        #  foreach(@reads) {
        #    $sum += $_;
        #  }
        #  my $avg = $sum / scalar(@reads);
        #  $read_count = 0;
        #  @reads = ();
        #  if($avg > 3.78) {
        #    $if0->write(chr(33));
        #    print "*"
        #  } else {
        #    $if0->write(chr(32));
        #  }
        #  print "$avg\n";
        #}
        usleep(10000);
        print "$addr - $minus\n";
    }
}
